package com.workManager.controller;

import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.exception.BusinessException;
import com.workManager.pojo.TbDirectEstimate;
import com.workManager.service.DirectEstimateService;
import com.workManager.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/1/16 0016.
 */
@Controller
@RequestMapping("/directEstimate")
public class DirectEstimateController extends BaseController{

    @Autowired
    private DirectEstimateService directEstimateService;


    @ApiOperation(value ="直接费用预估查询",notes = "直接费用预估查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(Integer projectId){
        Map<String, Object> map = new HashMap<String, Object>();
        String loginCode;
        try {
            loginCode = WebUtil.getCurrentUserVO().getLoginCode();
        }catch (Exception e){
            throw new BusinessException("未登录");
        }
        map.put("loginCode", loginCode);
        map.put("projectId", projectId);
        ModelMap modelMap = new ModelMap();
        return setSuccessModelMap(modelMap,directEstimateService.query(map));
    }

    @ApiOperation(value ="新增直接费用预估",notes = "新增直接费用预估")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody TbDirectEstimate directEstimate){
        directEstimate.setCreateDate(new Date());
        String loginCode;
        try {
            loginCode = WebUtil.getCurrentUserVO().getUserName();
        }catch (Exception e){
            throw new BusinessException("未登录");
        }
        directEstimate.setOperator(loginCode);
        ModelMap modelMap = new ModelMap();
        directEstimateService.add(directEstimate);
        return setSuccessModelMap(modelMap,"添加成功");
    }


    @ApiOperation(value ="作废",notes = "作废")
    @RequestMapping(value = "/invalid",method = RequestMethod.POST)
    public Object invalid(@RequestBody TbDirectEstimate directEstimate){
        ModelMap modelMap = new ModelMap();
        directEstimateService.invalid(directEstimate);
        return setSuccessModelMap(modelMap,"作废成功");
    }

}
